<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>Spring Boot WebSocket+广播式</title>
</head>
<body>
<noscript>
    <h2 style="color:#ff0000">貌似你的浏览器不支持websocket</h2>
</noscript>

<span id="viewing">0</span>人正在看

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">

    //观看人数统计
    var ws;
    var target = "ws://localhost:8888/content_websocket?content_id=9999";

    $(function () {

        //处理浏览器兼容性
        if ('WebSocket' in window) {
            ws = new WebSocket(target);
        } else if ('MozWebSocket' in window) {
            ws = new MozWebSocket(target);
        } else {
            alert('WebSocket is not supported by this browser.');
            return;
        }
        ws.onopen = function () {
            let contentId = Math.floor(Math.random() * 100)
            ws.send(contentId + '')
        };
        ws.onmessage = function (event) {
            $("#viewing").html(event.data);
        };

        ws.onclose = function (event) {

        }

        ws.onerror = function (event) {
            console.log(event);
        }

    })
</script>

</body>
</html>